package com.nifostasky.comic;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.nifostasky.db.DataBaseOpenHelper;
import com.nifostasky.fragment.MainFragment1;
import com.nifostasky.fragment.MainFragment2;
import com.nifostasky.fragment.MainFragment3;

public class MainActivity extends AppCompatActivity {
    //声明组件
    /*    private TextView mbtn_main1 =null;
        private TextView mbtn_main2 =null;*/

    private Button mbtn_main1 =null;
    private Button mbtn_main2 =null;
    private Button mbtn_main3 = null;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取组件
        mbtn_main1 = findViewById(R.id.btn_main1);
        mbtn_main2 = findViewById(R.id.btn_main2);
        mbtn_main3 =findViewById(R.id.btn_main3);

        //得到数据库帮助类
        DataBaseOpenHelper helper = new DataBaseOpenHelper(MainActivity.this);
        //创建数据库
        helper.getWritableDatabase();

        //默认页面  fragment1

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft =null;
        //得到fragment1
        MainFragment1 fragment1 = new MainFragment1();
        //事务
        ft = fm.beginTransaction();
        //fragment的内容填充main中预留的内容
        ft.replace(R.id.fl_mainPageContent,fragment1);
        ft.commit();



       //设置按键点击事件
        setListeners();
    }


//设置按键点击事件
    private void setListeners(){
        Listener listener = new Listener();
        mbtn_main1.setOnClickListener(listener);
        mbtn_main2.setOnClickListener(listener);
        mbtn_main3.setOnClickListener(listener);
    }


//点击事件实现
    private class Listener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = null;
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft =null;
            // 按键事件定义
            switch (v.getId()){
                case R.id.btn_main1:
                    //得到fragment1
                    MainFragment1 fragment1 = new MainFragment1();
                    //事务
                    ft = fm.beginTransaction();
                    //fragment的内容填充main中预留的内容
                    ft.replace(R.id.fl_mainPageContent,fragment1);
                    ft.commit();
                    break;
                case R.id.btn_main2:
                    //得到fragment1
                    MainFragment2 fragment2 = new MainFragment2();
                    //事务
                    ft = fm.beginTransaction();
                    //fragment的内容填充main中预留的内容
                    ft.replace(R.id.fl_mainPageContent,fragment2);
                    ft.commit();
                    break;
                case R.id.btn_main3:
                    //得到fragment1
                    MainFragment3 fragment3 = new MainFragment3();
                    //事务
                    ft = fm.beginTransaction();
                    //fragment的内容填充main中预留的内容
                    ft.replace(R.id.fl_mainPageContent,fragment3);
                    ft.commit();
                    break;
            }

            //执行意图进行跳转
            //startActivity(intent);
        }
    }
}
